home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / CDICTION / CSMARTED.C < prev    next >
Text File  |  1990-01-12  |  8KB  |  296 lines

  1. /*****************************************************************************
  2. CSmartEditText.c
  3.  
  4.     see header for information
  5.  
  6. SUPERCLASS = CEditText
  7. *****************************************************************************/
  8. #include "CSmartEditText.h"
  9. #include "CSmartDocument.h"
  10. #include "CSmartWindow.h"
  11. #include "Commands.h"
  12. #include "CSmartTETask.h"
  13.  
  14. /*****************************************************************************/
  15. void CSmartEditText::ISmartEditText(CView *anEnclosure, CBureaucrat *aSupervisor,
  16.                         Int16 aWidth, Int16 aHeight,
  17.                         Int16 aHEncl, Int16 aVEncl,
  18.                         SizingOption aHSizing, SizingOption aVSizing,
  19.                         Int16 aLineWidth, Int16 firstTaskIndex)
  20. /*
  21.     All parameters are standard, with the exception of firstTaskIndex.
  22.     This should give the index into the STR# 130 (STRtaskNames) resource
  23.     where the undo strings reside. Strings for cut,copy,paste,clear,type
  24.     commands are required (and in that order). firstTaskIndex gives the
  25.     index for cut.
  26.     TCL assumes all undo strings are located in this resource.
  27. */
  28. {
  29.     CSmartWindow    *window;
  30.     
  31.     /*    Get the window that (ultimately) encloses us. We can't assume 
  32.         that the window is our immediate enclosure or the document is
  33.         our immediate supervisor so we do something a little dirty
  34.         to get the window and document */
  35.     
  36.     window = (CSmartWindow *) GetWRefCon( anEnclosure->GetMacPort());
  37.     if (member( window, CSmartWindow))
  38.     {
  39.         /*    Since TextEdit will pick up its font characteristics
  40.             from the current port, we want to be sure the port is
  41.             in its preferred state (if it has one) before TENew()
  42.             is called. */
  43.     
  44.         window->RestoreEnvirons();
  45.         itsSmartDoc = (CSmartDocument *) window->itsSupervisor;
  46.     }
  47.     else itsSmartDoc = NIL;
  48.     
  49.     CEditText::IEditText( anEnclosure, aSupervisor, aWidth, aHeight, aHEncl,
  50.                 aVEncl, aHSizing, aVSizing, aLineWidth);
  51.         
  52.     if (itsSmartDoc) 
  53.         itsSmartDoc->AddEditItem( this);    /* register in doc's list */
  54.         
  55.     isKeyTarget = FALSE;
  56.     dirty = FALSE;
  57.     editable = TRUE;
  58.     passReturns = FALSE;
  59.     itsCurrentTask = NIL;
  60.     this->firstTaskIndex = firstTaskIndex;
  61.     GetSelection( &lastSelect.h, &lastSelect.v);
  62.  
  63. }    /* CSmartEditText::ISmartEditText */
  64. /*****************************************************************************/
  65. void CSmartEditText::DoCommand(Int32 theCommand)
  66. {
  67.     if ((theCommand >= cmdCut) && (theCommand <= cmdClear))
  68.     {
  69.         MakeTETask( (tTE_Command) theCommand - cmdCut);
  70.         inherited::DoCommand( theCommand);
  71.         itsSupervisor->Notify( itsCurrentTask);
  72.     }
  73.     else inherited::DoCommand( theCommand);
  74.  
  75. }    /* CSmartEditText::DoCommand */
  76. /*****************************************************************************/
  77. void CSmartEditText::DoClick(Point hitPt, short modifierKeys, long when)
  78. {
  79.     /* DoClick is not called unless we are editable */
  80.     extern CSmartEditText *gEditText;
  81.     
  82.     if ( !isKeyTarget && itsSmartDoc)
  83.     {
  84.         itsSmartDoc->ActivateEditItem( this);
  85.     }    
  86.     inherited::DoClick( hitPt, modifierKeys, when);
  87.  
  88.         
  89. }    /* CSmartEditText::DoClick */
  90. /*****************************************************************************/
  91. void CSmartEditText::DoKeyDown(char theChar, Byte keyCode, EventRecord *macEvent)
  92. {
  93.     /* we should never be the gopher if we're not editable
  94.         but just in case... */
  95.     if (!editable) 
  96.     {
  97.         itsSupervisor->DoKeyDown( theChar, keyCode, macEvent);
  98.         return;
  99.     }
  100.     
  101.     if (itsSmartDoc &&(theChar == kTab))
  102.     {
  103.         itsSmartDoc->ActivateNextEditItem( this);
  104.     }
  105.     else 
  106.     {
  107.         Boolean    madeNewTask = FALSE;
  108.         
  109.         if (((theChar == kCR)||(theChar == kEnter)) && passReturns)
  110.             itsSupervisor->DoKeyDown( theChar, keyCode, macEvent);
  111.         else
  112.         {
  113.             if ((theChar >= kClearKey) && (theChar <= kDownArrow))
  114.             {
  115.                 Point    sel;
  116.                 inherited::DoKeyDown( theChar, keyCode, macEvent);
  117.             }
  118.             else
  119.             {
  120.                 Point currSelect;
  121.                 
  122.                 GetSelection( &currSelect.h, &currSelect.v);
  123.                 if (!EqualPt( currSelect, lastSelect))
  124.                 {
  125.                     if (itsCurrentTask)
  126.                     {
  127.                         Notify( NIL);
  128.                         itsCurrentTask = NIL;
  129.                     }    
  130.                 }
  131.                 if (!itsCurrentTask)
  132.                 {
  133.                     MakeTETask( teType);
  134.                     madeNewTask = TRUE;
  135.                 }
  136.                 
  137.                 inherited::DoKeyDown( theChar, keyCode, macEvent);
  138.                 GetSelection( &lastSelect.h, &lastSelect.v);
  139.                 if (madeNewTask) itsSupervisor->Notify( itsCurrentTask);
  140.             }
  141.         }
  142.     }
  143.  
  144. }    /* CSmartEditText::DoKeyDown */
  145. /*****************************************************************************/
  146. void CSmartEditText::Hide( void)
  147. {
  148.     /* inherited::Hide will leave insertion point showing */
  149.     
  150.     if (active) 
  151.     {
  152.         if (itsSmartDoc)
  153.             itsSmartDoc->ActivateNextEditItem( this);
  154.         Deactivate();
  155.     }
  156.     inherited::Hide();
  157.     
  158. }    /* CSmartEditText::Hide */
  159. /*****************************************************************************/
  160. void CSmartEditText::SetTarget( Int16 isTarget)
  161. {
  162.     isKeyTarget = isTarget != 0;
  163.     
  164.     if (isKeyTarget) 
  165.     {
  166.         Activate();
  167.     }
  168.     else
  169.     {
  170.         Deactivate();
  171.         SetSelection( 0, 0);
  172.     }
  173.  
  174. }    /* CSmartEditText::SetTarget */
  175. /*****************************************************************************/
  176. void CSmartEditText::GetSelection( Int16 *startSel, Int16 *endSel)
  177. {
  178.     *startSel = (**macTE).selStart;
  179.     *endSel = (**macTE).selEnd;
  180.  
  181. }    /* CSmartEditText::GetSelection */
  182. /*****************************************************************************/
  183. void CSmartEditText::SetSelection( Int16 startSel, Int16 endSel)
  184. {
  185.     TESetSelect( startSel, endSel, macTE);
  186.  
  187. }    /* CSmartEditText::SetSelection */
  188. /*****************************************************************************/
  189. void CSmartEditText::SelectAll( void)
  190. {
  191.     SetSelection( 0, MAXINT);
  192.  
  193. }    /* CSmartEditText::SelectAll */
  194. /*****************************************************************************/
  195. void CSmartEditText::ClearText( void)
  196. {
  197.     Handle    h = NewHandle( 0);
  198.     
  199.     if (h)
  200.     {
  201.         SetTextHandle(h);
  202.         DisposHandle(h);
  203.     }
  204.  
  205. }    /* CSmartEditText::ClearText */
  206. /*****************************************************************************/
  207. Boolean    CSmartEditText::IsDirty( void)
  208. {
  209.     return dirty;
  210.  
  211. }    /* CSmartEditText::IsDirty */
  212. /*****************************************************************************/
  213. void CSmartEditText::SetDirty( Boolean dirty)
  214. {
  215.     this->dirty = dirty;
  216.  
  217. }    /* CSmartEditText::SetDirty */
  218. /*****************************************************************************/
  219. Int16 CSmartEditText::GetLineCount( void)
  220. {
  221.     Int16 numLines;
  222.     
  223.     numLines = (**macTE).nLines;
  224.     if ((!numLines)||(GetLastChar() == kCR)) numLines++;
  225.     return numLines;
  226.  
  227. }    /* CSmartEditText::GetLineCount */
  228. /*****************************************************************************/
  229. char CSmartEditText::GetLastChar( void) 
  230. {
  231.     return *(*(**macTE).hText + (**macTE).teLength - 1);
  232.     
  233. }    /* CSmartEditText::GetLastChar */
  234. /*****************************************************************************/
  235. void CSmartEditText::SetEditable( Int16 canEdit)
  236. {
  237.     editable = canEdit;
  238.     
  239.     if (!canEdit) 
  240.     {
  241.         if (isKeyTarget)
  242.         {
  243.             SetSelection( 0, 0);
  244.             itsSmartDoc->ActivateNextEditItem( this);
  245.         }
  246.     }
  247.     SetWantsClicks( editable);
  248.  
  249. }    /* CSmartEditText::SetEditable */
  250. /*****************************************************************************/
  251. Boolean CSmartEditText::IsEditable( void)
  252. {
  253.     return editable;
  254.  
  255. }    /* CSmartEditText::IsEditable */
  256. /*****************************************************************************/
  257. void CSmartEditText::SetPassReturns( Int16 passFlag)
  258. {
  259.     passReturns = passFlag;
  260.  
  261. }    /* CSmartEditText::SetPassReturns */
  262. /*****************************************************************************/
  263. Boolean CSmartEditText::GetPassReturns( void)
  264. {
  265.     return passReturns;
  266.  
  267. }    /* CSmartEditText::GetPassReturns */
  268. /*****************************************************************************/
  269. void CSmartEditText::GetString( StringPtr string)
  270. /*
  271.     returns first 255 chars (or less) as a pascal string.
  272. */
  273. {
  274.     Int16    length;
  275.     Handle    h;
  276.     
  277.     h = GetTextHandle();
  278.     length = MIN( 255, GetHandleSize(h));
  279.     string[0] = length;
  280.     BlockMove( *h, &string[1], length);
  281.  
  282. }    /* CSmartEditText::GetString */
  283. /*****************************************************************************/
  284. void CSmartEditText::MakeTETask( tTE_Command theCommand)
  285. {
  286.     CSmartTETask*    theTask = NIL;
  287.     
  288.  
  289.     theTask = new( CSmartTETask);
  290.     theTask->ISmartTETask( this, firstTaskIndex, theCommand);
  291.     itsCurrentTask = theTask;
  292.  
  293. }    /* CSmartEditText::MakeTETask */
  294. /*****************************************************************************/
  295.  
  296.